package com.zkca.xf.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseActivity;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.StringUtils;
import com.zkca.xf.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IpSettingActivity extends BaseActivity {

    @BindView(R.id.edit_ip)
    EditText editIp;
    @BindView(R.id.edit_duankou)
    EditText editDuankou;
    @BindView(R.id.queding)
    Button queding;
    //不更新的标志
    public static final int SHOW_NO_UPDATE_DIALOG = 1;
    //更新的标志
    public static final int SHOW_UPDATE_DIALOG = 2;

    private static boolean isExit = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ip_setting);
        ButterKnife.bind(this);
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtils.isEmpty(editIp.getText()) || StringUtils.isEmpty(editDuankou.getText())) {
                    ToastUtils.show(mContext, "请输入IP与端口号！");
                } else {
                    String ip = editIp.getText().toString();
                    if (!ipCheck(ip)) {
                        ToastUtils.show(mContext, "请输入合法IP格式");

                        return;
                    }

                    String port = editDuankou.getText().toString();
                    SPUtil.putString(mContext, Constants_SP.IP, ip);
                    SPUtil.putString(mContext, Constants_SP.PORT, port);//http://114.242.47.23:8083/
                    String servers = "http://" + ip + ":" + port + "/";
                    SPUtil.putString(mContext, Constants_SP.SERVER_ADDRESS, servers);
                    ConstantsXF.URL_SERVER_CE = servers;

                    gotoActivity(Activity_Login_Code.class, true);
                    finish();
                }
            }
        });
    }

    public static boolean ipCheck(String text) {
        if (text != null && !text.isEmpty()) { // 定义正则表达式
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            // 判断ip地址是否与正则表达式匹配
            if (text.matches(regex)) { // 返回判断信息
                return true;
            } else { // 返回判断信息
                return false;
            }
        }
        return false;
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private static Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_UPDATE_DIALOG:
                    break;
                case SHOW_NO_UPDATE_DIALOG:
                    break;
            }
            super.handleMessage(msg);
            //isExit = false;jianguanduao.
        }
    };
    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次后退键退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            //mHandler.sendEmptyMessageDelayed(0, 2000);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            //TODO: 2016/6/15 退出时清除登录信息
            //Log.e(TAG, "exit application");
            //IsLogin.isLoin = false;
            finish();
            MyApplication.getInstance().exitApp();
        }
    }


}
